package com.combetter.changxing.ui.base;

import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.android.databinding.library.baseAdapters.BR;
import com.combetter.changxing.R;
import com.combetter.changxing.ui.dialog.WaitingDialog;
import com.combetter.changxing.util.AppManager;

import java.io.Serializable;

/**
 * Created by 深圳唐龙飞讯科技有限公司 on 2018/5/2.
 */

/**
 * @param <T> 根据布局名自动创建的binding类；
 * @param <V> ViewModel类
 */
public abstract class BaseActivity<T extends ViewDataBinding, V extends AndroidViewModel> extends AppCompatActivity implements BaseUi<V> {
    protected T mBinding;

    protected V mViewModel;

    private WaitingDialog mWaitingDialog;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, setLayout());
        addStatusBar();
        initViewModel();
        init();
        loadData();
        AppManager.addActivity(this);
    }

    public void addStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //透明状态栏
            View decorView = getWindow().getDecorView();
            //让应用主题内容占用系统状态栏的空间,注意:下面两个参数必须一起使用 stable 牢固的
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //设置状态栏颜色为透明
            getWindow().setStatusBarColor(getResources().getColor(R.color.title_green));
        }

    }

    private void initViewModel() {
        try{
            Class<V> clzz = createViewModel();
            if (clzz != null) {
                mViewModel = (V) ViewModelProviders.of(this).get(createViewModel());
                //布局里的变量名必须有viewModel;
                mBinding.setVariable(BR.viewModel, mViewModel);
            }
        }catch (Exception e){
          e.printStackTrace();
        }

    }

    @Override
    public void startActivity(Class activityName, Parcelable value) {
        Intent intent = new Intent(this, activityName);
        intent.putExtra("value", value);
        startActivity(intent);
    }

    @Override
    public void startActivity(Class activityName, Serializable value) {
        Intent intent = new Intent(this, activityName);
        intent.putExtra("value", value);
        startActivity(intent);
    }

    /**
     * 返回bidng类
     *
     * @return
     */
    public T getBinding() {
        return mBinding;
    }

    public V getViewModel() {
        return mViewModel;
    }

    public void toFragment(int layoutId, Fragment toFragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(layoutId, toFragment)
                .commit();
    }

    protected void showWaitingDialog(String tip) {
        if (mWaitingDialog == null) {
            mWaitingDialog = new WaitingDialog(this);
        }
        mWaitingDialog.setWaitingText(tip);
        mWaitingDialog.setCanceledOnTouchOutside(false);
        mWaitingDialog.setCancelable(false);
        if (!mWaitingDialog.isShowing()) {
            mWaitingDialog.show();
        }
    }

    protected void dismissWaitingDialog() {
        if (mWaitingDialog != null && mWaitingDialog.isShowing()) {
            mWaitingDialog.dismiss();
        }
    }
}
